package org.jeecg.modules.smartSchoolTeacherLeave.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.jeecg.modules.smartSchoolScholarship.entity.SmartSchoolScholarship;
import org.jeecg.modules.smartSchoolStudentLeave.entity.SmartSchoolStudentLeave;
import org.jeecg.modules.smartSchoolTeacherLeave.entity.SmartSchoolTeacherLeave;
import org.jeecg.modules.smartSchoolTeacherLeave.mapper.SmartSchoolTeacherLeaveMapper;
import org.jeecg.modules.smartSchoolTeacherLeave.service.ISmartSchoolTeacherLeaveService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

/**
 * @Description: 教师请假表
 * @Author: jeecg-boot
 * @Date:   2022-06-04
 * @Version: V1.0
 */
@Service
public class SmartSchoolTeacherLeaveServiceImpl extends ServiceImpl<SmartSchoolTeacherLeaveMapper, SmartSchoolTeacherLeave> implements ISmartSchoolTeacherLeaveService {

    @Override
    public void saveOrUpdateInfo(JSONArray form_component_values, String originator_userid) {
        {
            SmartSchoolTeacherLeave teacher = new SmartSchoolTeacherLeave();
            for (int i = 0; i < form_component_values.size(); i++) {
                JSONObject jsonObject = form_component_values.getJSONObject(i);
                String name = jsonObject.getString("name");
                String value = jsonObject.getString("value");
                teacher.setLeaveType("未销假");
                switch (name){
                    case "开始时间":
                        teacher.setStartTime(value);
                        break;
                    case "结束时间":
                        teacher.setEndTime(value);
                        break;
                    case "时长":
                        teacher.setLongTime(value);
                        break;
                    case "姓名":
                        teacher.setTeacherName(value);
                        break;
                    case "工号":
                        teacher.setTeacherNum(value);
                        break;
                    case "请假事由":
                        teacher.setCause(value);
                        break;
                }
            }
            teacher.setId(originator_userid);

            List<SmartSchoolTeacherLeave> diyStudents = baseMapper.selectList(new LambdaQueryWrapper<SmartSchoolTeacherLeave>().eq(SmartSchoolTeacherLeave::getId, teacher.getId()));
            if (diyStudents!=null && diyStudents.size()>0){
                baseMapper.update(teacher,new LambdaQueryWrapper<SmartSchoolTeacherLeave>().eq(SmartSchoolTeacherLeave::getId,teacher.getId()));
            }else {
                baseMapper.insert(teacher);
            }
        }
    }

    @Override
    public void xiaojia(JSONArray form_component_values, String originator_userid) {
        SmartSchoolTeacherLeave teacher = new SmartSchoolTeacherLeave();
        teacher.setLeaveType("已销假");
        teacher.setId(originator_userid);
        List<SmartSchoolTeacherLeave> diyStudents = baseMapper.selectList(new LambdaQueryWrapper<SmartSchoolTeacherLeave>().eq(SmartSchoolTeacherLeave::getId, teacher.getId()));
        baseMapper.update(teacher,new LambdaQueryWrapper<SmartSchoolTeacherLeave>().eq(SmartSchoolTeacherLeave::getId,teacher.getId()));
    }
}
